
package com.ptr.ptrsystem.subirArchivos;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author WALDIR
 */

import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet(name = "SubirArchivosServlet", urlPatterns = {"/SubirArchivos"})
public class SubirArchivosServlet extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         
        boolean indicador=false;
        String NombreArchivo="";
        List Nlista=null;
        String Directorio=getServletContext().getRealPath("dos/");
        try {
            DiskFileItemFactory factory=new DiskFileItemFactory();
            factory.setSizeThreshold(4096);
            factory.setRepository(new File(Directorio + "/"));
            ServletFileUpload upload=new ServletFileUpload(factory);
            upload.setSizeMax(100000000);
            Nlista=upload.parseRequest(request);
            if(Nlista==null)
            {
             indicador=false;
            }
            Iterator i=Nlista.iterator();
            FileItem actual=null;
            if(i.hasNext())
            {
                actual=(FileItem)i.next();
                NombreArchivo=actual.getName();
                File fichero=new File(NombreArchivo);
                fichero=new File(Directorio+"/"+fichero.getName());
                actual.write(fichero);
                indicador=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         if (indicador) {          
            out.println("<h1>ARCHIVO SUBIDO CON EXITO</h1>");
        }else{
            out.println("NO C PUDO SUBIR EL ARCHIVO");
        }
    
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
